home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / program / qlib205.zip / QLIB.ZIP / SRC / STARTUP / MEMINIT.ASM < prev    next >
Assembly Source File  |  1997-01-09  |  2KB  |  87 lines

  1. ; see alloc.asm for details
  2.  
  3. .data?
  4. ; This is what each header looks like
  5. heads struct  ;12bytes
  6.   magic dw ? ; 'PQ' ;signature
  7.   flag db ?  ;bit0=free(1)  bit1=last(2)   (1==true)
  8.   _a1 db ?   ;junk byte for alignment (not used!)
  9.   prev dd ?  ;ptr=>previous block
  10.   siz dd ?   ;size of this block
  11. heads ends
  12.  
  13. externdef _baseram:dword
  14. _baseram dd ? ;RAM base (relative)
  15. _handle dd ?  ;RAM handle  (used for PMODE/W only)
  16.  
  17. .code
  18.  
  19. alloc_init proc private
  20.   local siz:dword
  21.   sub esp,48
  22.   mov ax,0500h
  23.   mov edi,esp
  24.   int 31h
  25.   jc outofram
  26.   mov eax,[esp]  ;RAM avail
  27.   add esp,48                               ;FIX : v2.00 Beta #2 : this was
  28.   .if eax>HEAP_MAX   ;too much?            ;before the mov eax,[edi]
  29.     mov eax,HEAP_MAX
  30.   .endif
  31.   ;round to highest 4K
  32.   .if eax & 0fffh
  33.     and eax,0fffff000h
  34.     add eax,1000h
  35.   .endif
  36.   mov siz,eax
  37.   .if !eax
  38.     mov _baseram,0  ;no XMS wanted
  39.     ret
  40.   .endif
  41. ; under Windoze and DOS/4GW sometimes more RAM was said to be avail
  42. ; than there really is.  So I slowly decrease the amount requested until
  43. ; it works or until it drops below HEAP_MIN
  44.  
  45. retry:
  46.   .if eax<HEAP_MIN
  47.     jmp outofram
  48.   .endif
  49.   mov ebx,eax
  50.   shr ebx,16
  51.   mov cx,ax
  52.   mov ax,0501h
  53.   int 31h
  54.   .if carry?
  55.     .if siz<64*1024+1
  56.       jmp outofram
  57.     .endif
  58.     sub siz,64*1024
  59.     mov eax,siz
  60.     jmp retry
  61.   .endif
  62.   shl ebx,16
  63.   mov bx,cx
  64.   mov _baseram,ebx
  65.   shl esi,16
  66.   mov si,di
  67.   mov _handle,esi
  68.  
  69.   mov eax,siz  ;FIX : v2.00 Beta #2 : This was not here !?!
  70.   mov word ptr[ebx].heads.magic,5150h  ;'PQ'=50,51
  71.   mov byte ptr[ebx].heads.flag,3h      ;last&free
  72.   mov dword ptr[ebx].heads.prev,0      ;no prev!
  73.   sub eax,sizeof heads
  74.   mov dword ptr[ebx].heads.siz,eax
  75.   ret
  76. alloc_init endp
  77.  
  78. outofram:
  79.   callp print,"Error:Out of memory...\n"
  80.   mov eax,HEAP_MIN
  81.   add eax,1023
  82.   shr eax,10  ;divide by 1k
  83.   callp printf,"Require at least %uk XMS\n",eax
  84.   mov ax,4c03h
  85.   int 21h
  86.  
  87.